﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UWP.Douban.Utils;

namespace UWP.Douban.Models
{
    public class Topic
    {
        [JsonProperty("updated")]
        public DateTime Updated { get; set; }

        [JsonProperty("like_count")]
        public int LikeCount { get; set; }

        [JsonProperty("alt")]
        public string Alt { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("locked")]
        public bool Locked { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("share_url")]
        public string ShareUrl { get; set; }

        [JsonProperty("created")]
        public DateTime Created { get; set; }

        [JsonProperty("content")]
        public string Content { get; set; }

        public string ContentHtml
        {
            get
            {
                Regex regex = new Regex(@"<图片\d+>");

                if (string.IsNullOrEmpty(Content)) return string.Empty;
                var contentHtml = Content.Replace("\r\n", "</br>");

                var photos = Photos.ToList();
                contentHtml = regex.Replace(contentHtml, match =>
                {
                    int index = int.Parse(match.Value.Replace("<图片", "").TrimEnd('>'));

                    if (index < photos.Count && index > 0)
                    {
                        var photo = photos[index - 1];
                        photos.Remove(photo);
                        if (photo != null)
                        {
                            return $"<img  src='{photo.Alt}'  alt='{photo.Title}' />";
                        }
                    }
                    return "";
                });
                string images = "";  //剩余的照片
                if (Photos.Any())
                {
                    images = string.Join("</br>", photos.Select(photo => $"<img  src='{photo.Alt}'  alt='{photo.Title}' />"));
                }

                return $"<html><head><meta charset='UTF-8'><style>body, html{{overflow-x:hidden;    line-height: 1.8!important;}} img{{width:100%;display: block;display: block;}}</style></head> {contentHtml}</br>{images}</html>";
            }
        }

        [JsonProperty("comments_count")]
        public int CommentsCount { get; set; }

        [JsonProperty("author")]
        public User Author { get; set; }

        [JsonProperty("group")]
        public Group Group { get; set; }

        [JsonProperty("photos")]
        public List<Photo> Photos { get; set; }

        public string UpdatedDescription => DateTimeUtil.Description(Updated);
    }

    public class Group
    {
        [JsonProperty("domain")]
        public string Domain { get; set; }

        [JsonProperty("uid")]
        public string Uid { get; set; }

        [JsonProperty("member_role")]
        public string MemberRole { get; set; }

        [JsonProperty("owner")]
        public User Owner { get; set; }

        [JsonProperty("admins")]
        public List<User> Admins { get; set; }

        [JsonProperty("alt")]
        public string Alt { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("desc")]
        public string Description { get; set; }

        [JsonProperty("member_count")]
        public int MemberCount { get; set; }

        [JsonProperty("created")]
        public DateTime Created { get; set; }

        [JsonProperty("join_type")]
        public string JoinType { get; set; }

        [JsonProperty("admin_role_name")]
        public string AdminRoleName { get; set; }

        [JsonProperty("member_role_name")]
        public string MemberRoleName { get; set; }

        [JsonProperty("large_avatar")]
        public string LargeAvatar { get; set; }

        [JsonProperty("avatar")]
        public string Avatar { get; set; }
    }

    public class PhotoSize
    {
        [JsonProperty("width")]
        public int Width { get; set; }

        [JsonProperty("height")]
        public int Height { get; set; }
    }

    public class Photo
    {
        [JsonProperty("size")]
        public PhotoSize Size { get; set; }

        [JsonProperty("alt")]
        public string Alt { get; set; }

        [JsonProperty("layout")]
        public string Layout { get; set; }

        [JsonProperty("topic_id")]
        public string TopicId { get; set; }

        [JsonProperty("seq_id")]
        public string SeqId { get; set; }

        [JsonProperty("author_id")]
        public string AuthorId { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("creation_date")]
        public DateTime CreationDate { get; set; }
    }

    public class Comment
    {
        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("vote_count")]
        public int VoteCount { get; set; }

        [JsonProperty("author")]
        public User Author { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("time")]
        public DateTime Time { get; set; }

        [JsonProperty("ispopular")]
        public bool IsPopular { get; set; }

        [JsonProperty("quote_comment")]
        public Comment QuoteComment { get; set; }


        public string CreatedDescription
        {
            get { return DateTimeUtil.Description(Time); }
        }

        public string VoteDescription
        {
            get
            {
                if (VoteCount == 0)
                    return "赞";
                else
                {
                    return $"赞({VoteCount})";
                }
            }
        }

        public string ContentHtml
        {
            get
            {
                return Text;
            }
        }
    }

    public class User
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("is_suicide")]
        public string IsSuicide { get; set; }

        [JsonProperty("avatar")]
        public string Avatar { get; set; }

        [JsonProperty("uid")]
        public string Uid { get; set; }

        [JsonProperty("alt")]
        public string Alt { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("loc_name")]
        public string Location { get; set; }


        [JsonProperty("desc")]
        public string Description { get; set; }

        [JsonProperty("large_avatar")]
        public string LargeAvatar { get; set; }
    }

    public class UserInfo
    {
        [JsonProperty("user")]
        public User User { get; set; }
        [JsonProperty("rec_topic_count")]
        public int RecTopicCount { get; set; }

        [JsonProperty("reply_topic_count")]
        public int ReplyTopicCount { get; set; }

        [JsonProperty("album_count")]
        public int AlbumCount { get; set; }

        [JsonProperty("join_group_count")]
        public int JoinGroupCount { get; set; }

        [JsonProperty("join_groups")]
        public GroupInfo GroupInfo { get; set; }

        [JsonProperty("topic_count")]
        public int TopicCount { get; set; }

        [JsonProperty("like_topic_count")]
        public int LikeTopicCount { get; set; }
    }

    public class GroupInfo
    {
        [JsonProperty("count")]
        public int Count { get; set; }

        [JsonProperty("start")]
        public int Start { get; set; }

        [JsonProperty("total")]
        public int Total { get; set; }

        [JsonProperty("groups")]
        public List<Group> Groups { get; set; }
    }


    public class DouLastMail
    {
        [JsonProperty("target")]
        public User Target { get; set; }

        [JsonProperty("last_mail")]
        public Doumail LastMail { get; set; }

        [JsonProperty("target_id")]
        public int TargetId { get; set; }

        [JsonProperty("last_mail_id")]
        public int LastMailId { get; set; }

        [JsonProperty("unread_count")]
        public int Unread_Count { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("id")]
        public int Id { get; set; }
    }

    public class Doumail
    {

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("receiver_id")]
        public string ReceiverId { get; set; }

        public User ReceiverUser { get; set; }


        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("sender_id")]
        public string SenderId { get; set; }

        public User SenderUser { get; set; }

        [JsonProperty("content")]
        public string Content { get; set; }

        [JsonProperty("published")]
        public DateTime Published { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }
    }

    public class DoumailThread
    {
        [JsonProperty("count")]
        public int Count { get; set; }

        [JsonProperty("start")]
        public int Start { get; set; }

        [JsonProperty("users")]
        private dynamic Users { get; set; }

        public List<User> MailUsers
        {
            get
            {
                List<User> users = new List<User>();
                JObject o = (JObject)Users;
                foreach (JToken item in o.Children())
                {
                    var user = JsonConvert.DeserializeObject<User>(item.First().ToString());
                    users.Add(user);
                }

                return users;
            }
        }

        [JsonProperty("mails")]
        public List<Doumail> Doumails { get; set; }
    }
}
